import Toast from "tdesign-miniprogram/toast/index";

const app = getApp();

const AuthStepType = {
  ONE: 1,
  TWO: 2,
  THREE: 3,
};

const menuData = [
  [
    {
      title: "收货地址",
      tit: "",
      url: "",
      type: "address",
    },
    {
      title: "优惠券",
      tit: "",
      url: "",
      type: "coupon",
    },
    {
      title: "积分",
      tit: "",
      url: "",
      type: "point",
    },
  ],
  [
    {
      title: "帮助中心",
      tit: "",
      url: "",
      type: "help-center",
    },
    {
      title: "客服热线",
      tit: "",
      url: "",
      type: "service",
      icon: "service",
    },
  ],
];

const orderTagInfos = [
  {
    title: "待付款",
    iconName: "wallet",
    orderNum: 0,
    tabType: 5,
    status: 1,
  },
  {
    title: "待发货",
    iconName: "deliver",
    orderNum: 0,
    tabType: 10,
    status: 1,
  },
  {
    title: "待收货",
    iconName: "package",
    orderNum: 0,
    tabType: 40,
    status: 1,
  },
  {
    title: "待评价",
    iconName: "comment",
    orderNum: 0,
    tabType: 60,
    status: 1,
  },
  {
    title: "退款/售后",
    iconName: "exchang",
    orderNum: 0,
    tabType: 0,
    status: 1,
  },
];

const getDefaultData = () => ({
  showMakePhone: false,
  userInfo: {},
  menuData,
  orderTagInfos,
  customerServiceInfo: {},
  currAuthStep: AuthStepType.ONE,
  showKefu: true,
  versionNo: "",
});

Page({
  data: getDefaultData(),

  onLoad() {
    this.getVersionInfo();
  },

  onShow() {
    this.getTabBar().init();
    this.init();
  },
  onPullDownRefresh() {
    this.init();
  },

  init() {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        currAuthStep: AuthStepType.TWO,
      });
    }
  },

  handleLogin() {
    // 显示加载中
    wx.showLoading({
      title: "登录中",
    });
    // 调用云函数
    wx.cloud.callFunction({
      name: "login",
      success: (res) => {
        wx.hideLoading();
        const userInfo = res.result;
        console.log("userInfo: ", userInfo);
        // 将用户信息存储在全局变量和缓存中
        const app = getApp();
        app.globalData.userInfo = userInfo;
        wx.setStorageSync("userInfo", userInfo);
        this.setData({ currAuthStep: AuthStepType.TWO, userInfo });
      },
      fail: (err) => {
        wx.hideLoading();
        console.error("云函数调用失败", err);
      },
    });
  },

  onClickCell({ currentTarget }) {
    const { type } = currentTarget.dataset;

    switch (type) {
      case "address": {
        wx.navigateTo({ url: "/pages/user/address/list/index" });
        break;
      }
      case "service": {
        this.openMakePhone();
        break;
      }
      case "help-center": {
        Toast({
          context: this,
          selector: "#t-toast",
          message: "你点击了帮助中心",
          icon: "",
          duration: 1000,
        });
        break;
      }
      case "point": {
        Toast({
          context: this,
          selector: "#t-toast",
          message: "你点击了积分菜单",
          icon: "",
          duration: 1000,
        });
        break;
      }
      case "coupon": {
        wx.navigateTo({ url: "/pages/coupon/coupon-list/index" });
        break;
      }
      default: {
        Toast({
          context: this,
          selector: "#t-toast",
          message: "未知跳转",
          icon: "",
          duration: 1000,
        });
        break;
      }
    }
  },

  jumpNav(e) {
    const status = e.detail.tabType;

    if (status === 0) {
      wx.navigateTo({ url: "/pages/order/after-service-list/index" });
    } else {
      wx.navigateTo({ url: `/pages/order/order-list/index?status=${status}` });
    }
  },

  jumpAllOrder() {
    wx.navigateTo({ url: "/pages/order/order-list/index" });
  },

  openMakePhone() {
    this.setData({ showMakePhone: true });
  },

  closeMakePhone() {
    this.setData({ showMakePhone: false });
  },

  call() {
    wx.makePhoneCall({
      phoneNumber: this.data.customerServiceInfo.servicePhone,
    });
  },

  gotoUserEditPage() {
    const { currAuthStep } = this.data;
    console.log('currAuthStep: ', currAuthStep);

    if (currAuthStep === AuthStepType.ONE) {
      this.handleLogin();
    } else if (currAuthStep === AuthStepType.TWO) {
      wx.navigateTo({ url: "/pages/user/person-info/index" });
    }
  },

  getVersionInfo() {
    const versionInfo = wx.getAccountInfoSync();
    const { version, envVersion = __wxConfig } = versionInfo.miniProgram;
    this.setData({
      versionNo: envVersion === "release" ? version : envVersion,
    });
  },
});
